
const { query } = require('../db/mysql')

class Category {
  constructor () {
    this.tableName = 'category'
    this.relateName = 'cat_tag'
  }

  getList (params = {}) {
    const sql = `SELECT ${this.tableName}.id, ${this.tableName}.name, ${this.tableName}.status
                   FROM ${this.tableName} where status = 1
                     limit ?,?`
    return query(sql, [((params.page - 1) * params.page_size), params.page_size])
  }

  getCateRelIds (options = {}) {
    const categoryId = options.categoryId || 0
    const sql = `SELECT ${this.relateName}.tag_id FROM ${this.relateName} WHERE cat_id = ? AND ${this.relateName}.status = 1`
    return query(sql, [categoryId])
  }

  getCateListTagIds (options = {}) {
    const sql = `SELECT ${this.relateName}.tag_id FROM ${this.relateName} WHERE cat_id IN (?) AND ${this.relateName}.status = 1`
    return query(sql, [options.cat_ids])
  }
}

module.exports = new Category()
